/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.edu.mobileWeb.course;

import com.idenger.modules.edu.entity.common.BaseResponseMsg;
import com.idenger.modules.edu.entity.common.ResponseMsg;
import com.idenger.modules.edu.entity.course.EduCourse;
import com.idenger.modules.edu.service.practice.EduPracticeService;
import com.idenger.modules.edu.web.comm.Constant;
import com.idenger.common.persistence.Page;
import com.idenger.common.utils.StringUtils;
import com.idenger.common.web.BaseController;
import com.idenger.modules.edu.entity.course.EduVideoHistory;
import com.idenger.modules.edu.entity.enroll.EduEnroll;
import com.idenger.modules.edu.entity.user.EduUser;
import com.idenger.modules.edu.service.course.EduCourseContentService;
import com.idenger.modules.edu.service.course.EduCourseService;
import com.idenger.modules.edu.service.course.EduSectionService;
import com.idenger.modules.edu.service.course.EduVideoHistoryService;
import com.idenger.modules.edu.service.enroll.EduEnrollService;
import com.idenger.modules.edu.service.exercisehistory.EduExerciseHistoryService;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 视频历史Controller
 * @author 视频历史
 * @version 2017-12-13
 */
@Controller
@RequestMapping(value = "${mobilePath}/edu/course/eduVideoHistory")
public class MEduVideoHistoryController extends BaseController {

	@Autowired
	private EduVideoHistoryService eduVideoHistoryService;
	@Autowired
	private EduEnrollService eduEnrollService;
	
	@Autowired
	private EduCourseService eduCourseService;

	@ModelAttribute
	public EduVideoHistory get(@RequestParam(required=false) String id) {
		EduVideoHistory entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eduVideoHistoryService.get(id);
		}
		if (entity == null){
			entity = new EduVideoHistory();
		}
		return entity;
	}

	@RequiresPermissions("edu:course:eduVideoHistory:view")
	@RequestMapping(value = {"list", ""})
	public String list(EduVideoHistory eduVideoHistory, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EduVideoHistory> page = eduVideoHistoryService.findPage(new Page<EduVideoHistory>(request, response), eduVideoHistory); 
		model.addAttribute("page", page);
		return "modules/edu/course/eduVideoHistoryList";
	}

	@RequiresPermissions("edu:course:eduVideoHistory:view")
	@RequestMapping(value = "form")
	public String form(EduVideoHistory eduVideoHistory, Model model) {
		model.addAttribute("eduVideoHistory", eduVideoHistory);
		return "modules/edu/course/eduVideoHistoryForm";
	}

	//保存观看历史，点击了观看视频,拿到视频的分值

	@RequestMapping(value = "saveVideoHistory")
	public String saveVideoHistory(EduVideoHistory eduVideoHistory , HttpServletRequest request, HttpServletResponse response, Model model, RedirectAttributes redirectAttributes) {
		BaseResponseMsg baseResponseMsg;
		EduUser eduUser = (EduUser) request.getAttribute("currentUser");
		eduVideoHistory.setUserId(eduUser.getId());
		eduVideoHistory.setStatus(Constant.VIDEO_STATUS_YES);
		List<EduVideoHistory> eduVideoHistories=eduVideoHistoryService.findByUserIdAndcourseContentId(eduVideoHistory);
		if(eduVideoHistories.size()>0){
			eduVideoHistory.setId(eduVideoHistories.get(0).getId());
		}
		eduVideoHistoryService.save(eduVideoHistory);
		//比较分值 计算有效分 分值是否达到结课的标准
		compareScore(eduVideoHistory,eduUser);
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData("eduVideoHistory",eduVideoHistory);
		return renderString(response, baseResponseMsg);
	}
	//是否结课成功
	public void compareScore(EduVideoHistory eduVideoHistory,EduUser eduUser){
		//计算分值，改报名报状态
		EduCourse eduCourse =  eduCourseService.getCourse(eduVideoHistory.getCourseContentId());
		EduEnroll eduEnroll2 = new EduEnroll();
		eduEnroll2.setUserid(eduUser.getId());
		eduEnroll2.setCurriculumId(eduCourse.getId());
		EduEnroll enSta = eduEnrollService.getEduEnrollByUserIdAndCourseId(eduEnroll2);
		if((Constant.EDUENROLL_STATUS_NO).equals(enSta.getStatus())){
			BigDecimal percentage = new BigDecimal("100");
			String sumScore1 ="0";
			if(StringUtils.isNotBlank(eduCourse.getSumScore())){
				sumScore1 = eduCourse.getSumScore();
			}
			//拿到视频有效分
			BigDecimal videoValidScore = getVideoValidScore(eduCourse,percentage,eduUser);
			//拿到练习的有效分
			BigDecimal practiceValidScore = getPracticeValidScore(eduCourse,percentage,eduUser);
			//实际有效总分
			BigDecimal sumValidScore =videoValidScore.add(practiceValidScore);
			BigDecimal sumScore = new BigDecimal(sumScore1);
			sumScore=sumScore.setScale(2, BigDecimal.ROUND_DOWN); 
			if(sumScore.compareTo(sumValidScore)==1){
				//不结课
			}else{
				//分值达到，可以结课，
				EduEnroll eduEnroll = new EduEnroll();
				eduEnroll.setCurriculumId(eduCourse.getId());
				eduEnroll.setUserid(eduUser.getId());
				EduEnroll enroll =	eduEnrollService.getEduEnrollByUserIdAndCourseId(eduEnroll);
				if(enroll!= null){
					enroll.setStatus(Constant.EDUENROLL_STATUS_IS_OK);
					eduEnrollService.save(enroll);
				}
			}
		}
	}
	//视频有效分
	public BigDecimal getVideoValidScore(EduCourse eduCourse,BigDecimal percentage,EduUser eduUser){
		//视频
		String videoScore = "0";
		if(StringUtils.isNotBlank(eduCourse.getVideoScore())){
			videoScore = eduCourse.getVideoScore();
		}
		//拿到视频占比
		BigDecimal videoValidScorePercentage = new BigDecimal(videoScore);
		videoValidScorePercentage = videoValidScorePercentage.divide(percentage, 2, RoundingMode.HALF_UP);
		//拿到视频的有效分
		String videoValidScore1= eduCourseService.getVideoValidScore(eduUser.getId(),eduCourse.getId());
		if(StringUtils.isBlank(videoValidScore1)){
			videoValidScore1 = "0";
		}
		BigDecimal videoValidScore=new BigDecimal(videoValidScore1);
		videoValidScore=videoValidScore.setScale(2, BigDecimal.ROUND_DOWN); 
		videoValidScore = videoValidScore.multiply(videoValidScorePercentage);	
		return videoValidScore;
	}
	
	//课程有效分
	public BigDecimal getPracticeValidScore(EduCourse eduCourse,BigDecimal percentage,EduUser eduUser){
		//课程
		String practiceScore = "0";
		if(StringUtils.isNotBlank(eduCourse.getPracticeScore())){
			practiceScore = eduCourse.getPracticeScore();
		}
		//拿到课程占比
		BigDecimal practiceValidScorePercentage=new BigDecimal(practiceScore);
		practiceValidScorePercentage = practiceValidScorePercentage.divide(percentage, 2, RoundingMode.HALF_UP);
		//拿到练习的有效分
		
		String practiceValidScore1 = eduCourseService.getPracticeValidScore(eduUser.getId(),eduCourse.getId());
		if(StringUtils.isBlank(practiceValidScore1)){
			practiceValidScore1="0";
		}
		BigDecimal practiceValidScore=new BigDecimal(practiceValidScore1);
		practiceValidScore=practiceValidScore.setScale(2, BigDecimal.ROUND_DOWN); 
		practiceValidScore = practiceValidScore.multiply(practiceValidScorePercentage);	
		return practiceValidScore;
	}
}